//
//  ViewController.swift
//  SSComment
//
//  Created by 松松 on 04/12/2019.
//  Copyright (c) 2019 松松. All rights reserved.
//

import UIKit
import SSComment
import Alamofire


class ViewController: UIViewController {

    let dataSource = ["添加评论","获取评论回复列表","获取回复带评论列表","删除评论","点赞增加","判断是否点赞","取消点赞","添加评分","获取某个商品所有评价","获取某个商品的所有评分统计","获取某个人的所有评论","按分类查询所有评分"]
    let viewModel = SSCommentViewModel()
    let APP_CODE = "10010013"

   

    override func viewDidLoad() {
        super.viewDidLoad()
        print("didLoad")
        let tableView = UITableView(frame: self.view.bounds)
        tableView.separatorInset = UIEdgeInsets.zero
        tableView.delegate = self
        tableView.dataSource = self
        view.addSubview(tableView)

    }

    @objc func longPressCell(sender: UILongPressGestureRecognizer){



        let alert = PopoverViewController()
        alert.preferredContentSize = CGSize(width: 200, height: 50)
        alert.modalPresentationStyle = .popover

        let popPresenter = alert.popoverPresentationController
        popPresenter?.sourceView = sender.view!
        popPresenter?.sourceRect = sender.view!.bounds
        popPresenter?.backgroundColor = UIColor.gray
        popPresenter?.delegate = self
        self.present(alert, animated: true, completion: nil)
    }



}

extension ViewController : UIPopoverPresentationControllerDelegate{
    func adaptivePresentationStyle(for controller: UIPresentationController) -> UIModalPresentationStyle {
        return .none
    }
}

extension ViewController: UITableViewDelegate,UITableViewDataSource{

    //MARK:tableView代理&数据源
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return 40
    }

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return dataSource.count
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        var cell = tableView.dequeueReusableCell(withIdentifier: "CellId")
        if cell == nil{
            cell = UITableViewCell.init(style: .default, reuseIdentifier: "CellId")
        }
        cell?.tag = indexPath.row
        let longPress = UILongPressGestureRecognizer(target: self, action: #selector(longPressCell(sender:)))
        cell?.addGestureRecognizer(longPress)
        cell?.textLabel?.text = dataSource[indexPath.row]

        return cell!
    }



    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        let title = dataSource[indexPath.row]

        switch title {
        case "添加评论":
            viewModel.addComment(appCode: APP_CODE,
                                 fromUserName: "张十三",
                                 fromUserHead: "http://pic102.nipic.com/file/20160624/22734439_173946745000_2.jpg",
                                 formUserId: "10000",
                                 commentImages: "http://b-ssl.duitang.com/uploads/item/201706/22/20170622131955_h4eZS.thumb.700_0.jpeg",
                                 replyCommentId: nil,
                                 commentId: "1133648865528713218", mainTypeCode: "1",
                                 toItemCode: "12",
                                 commentContent: "色是评论",
                                 success: { (modelData) in

            }) { (error) in

            }
        case "获取评论回复列表":
            viewModel
                .getCommentList(appCode: APP_CODE,
                                replyCommentId: nil,
                                mainTypeCode: "1",
                                toItemCode: "12",
                                commentSate: 1,
                                pageSize: 10,
                                pageNumber: 1,
                                success: { (modelData) in

            }) { (error) in

            }
        case "获取回复带评论列表":
            viewModel
                .getCommentReplayList(appCode: APP_CODE,
                                      replyCommentId: nil,
                                      mainTypeCode: "1",
                                      toItemCode: "12",
                                      commentSate: 1,
                                      pageSize: 10,
                                      pageNumber: 1,
                                      success: { (modelData) in

                }) { (error) in

            }
        case "删除评论":
            viewModel
                .deleteComment(appCode: APP_CODE,
                               mainTypeCode: "1",
                               toItemCode: "12",
                               id: 10000,
                               formUserId: "1127834465660166145",
                               success: { (modelData) in

                }) { (error) in

            }
        case "点赞增加":
            viewModel
                .like(appCode: APP_CODE,
                      fromUserName: "1",
                      fromUserHead: "http://pic102.nipic.com/file/20160624/22734439_173946745000_2.jpg",
                      toItemCode: "12",
                      goodType: 1,
                      mainTypeCode: "1",
                      formUserId: "1127834465660166145",
                      success: { (modelData) in

                }) { (error) in

            }
        case "判断是否点赞":
            viewModel
                .checkIsLike(appCode: APP_CODE, toItemCode: "12", mainTypeCode: "1", formUserId: "1127834465660166145", success: { (modelData) in

                }) { (error) in

            }
        case "取消点赞":
            viewModel
                .cancelLike(appCode: APP_CODE ,
                            toItemCode: "12",
                            mainTypeCode: "1",
                            formUserId: "1127834465660166145",
                            success: { (modelData) in

                }) { (error) in

            }
        case "添加评分":
            viewModel
                .addScore(appCode: APP_CODE, toItemCode: "12", formUserId: "10000", mainTypeCode: "1", fromUserName: "张十三", fromUserHead: "http://b-ssl.duitang.com/uploads/item/201706/22/20170622131955_h4eZS.thumb.700_0.jpeg", score: 10, textString: "1dsf", merchantId: "10", extensions: "色是评论", success: { (modelData) in

                }) { (error) in

            }
        case "获取某个商品所有评价":
            viewModel
                .getGoodComments(appCode: APP_CODE, toItemCode: "12", scoreLevel: 0, mainTypeCode: "1", pageSize: 10, pageNumber: 1, commentSate: 1, success: { (modelData) in

                }) { (error) in

            }
        case "获取某个商品的所有评分统计":
            viewModel
                .getGoodScores(appCode: APP_CODE, toItemCode: 12, mainTypeCode: "1", success: { (modelData) in

                }) { (error) in

            }
        case "获取某个人的所有评论":
            viewModel
                .getUserAllComments(appCode: APP_CODE, toItemCode: "12", formUserId: "10000", mainTypeCode: "1", pageSize: 10, pageNumber: 1, success: { (modelData) in

                }) { (error) in
            }
        case "按分类查询所有评分":
            viewModel
                .getScoreByClass(appCode: APP_CODE, mainTypeCode: "1", pageSize: 10, pageNumber: 1, success: { (modelData) in

                }) { (error) in

            }

        default:
            break
        }
    }

}

